# _*_coding: UTF-8 _*_
# author:jamesfeng2009
# time:2024/2/12 19:06
# ？？
import requests
import json

API_KEY = "sOFrISNZi3NcVC51u4iYZNk5"
SECRET_KEY = "DZMdqDT7PP2ti8rbfLRLGZgG84DmGNTn"


def main() -> None:
    url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=" + get_access_token()
    payload = json.dumps({
        "messages": [
            {
                "role": "user",
                "content": "你好"
            },
            {
                "role": "assistant",
                "content": "你好！我是一名人工智能语言模型，可以回答各种问题。请问有什么我可以帮助你的吗？"
            },
            {
                "role": "user",
                "content": "我是小红"
            }
        ]
    })

    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    print(response.text)


def get_access_token() -> str:
    """
    使用AK, SK生成鉴权签名(AccessToken)
    :return:  access_token
    """
    url = "https://aip.baidubce.com/oauth/2.0/token"
    params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
    print(str(requests.post(url, params=params).json().get("access_token")))
    return str(requests.post(url, params=params).json().get("access_token"))


if __name__ == '__main__':
    main()

